<?php
/**
 * 获取类详情
 * User: Administrator
 * Date: 2018\9\8 0008
 * Time: 10:09
 */

namespace app\Lib\Model\Role\RoleManage;


use app\Lib\InterfaceIn\Role\RoleHandle;
use app\Lib\InterfaceIn\Role\RoleManage\GetRoleInfoHandle;
use app\Lib\Model\AdminAuth\GetAuthorization\GetRoleAuth;
use app\Lib\Model\DbModel\Role;

class GetRoleInfo implements GetRoleInfoHandle
{
    /** @var RoleHandle $role */
    private $role;
    /**
     * 初始化
     * GetRoleInfoHandle constructor.
     * @param RoleHandle $role
     */
    public function __construct(RoleHandle $role)
    {
        $this->role = $role;
    }


    /**
     * 执行
     * @return array
     */
    public function run()
    {
        $model = new Role();
        $auth = new GetRoleAuth($this->role);
        $obj  = $auth->run();
        $roleAuth = [];
        foreach ($obj as $v){
            /**@var \app\Lib\Model\AdminAuth\MenuAuth $v */
            $roleAuth[] = $v->getMenuId();
        }
        $parent = 0;
        if($this->role->getParentClass() instanceof  \app\Lib\Model\Role\Role){
            $parent = $this->role->getParentClass()->getRoleId();
        }
        $data = [
            $model->_id=>$this->role->getRoleId(),
            $model->_name=>$this->role->getName(),
            $model->_list_order=>$this->role->getListOrder(),
            $model->_parent_id=>$parent,
            $model->_role_alias=>$this->role->getRoleAlias(),
            $model->_remark=>$this->role->getRemark(),
            $model->_is_admin_login=>$this->role->isAdminLogin(),
            'auth'=>json_encode($roleAuth)
        ];
        return $data;
    }


}